﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;


namespace AIS.SharePoint.AspNetFBA.Code
{
    public class ConfigSettings
    {

        private const string _AUTOGENWELCOMEEMAIL = "AISFBA_AutoGenWelcomeEMail";
        private const string _AUTOGENNEWUSERPASSSWORD = "AISFAB_AutoGeneratePassword";
        private const string _ENABLEPASSWORDRESET = "AISFAB_EnablePasswordReset";
        private const string _WELCOMEEMAILSUBJECT = "AISFAB_WelcomeEMailSubject";
        private const string _WELCOMEEMAILBODY = "AISFAB_WelcomeEMailBody";
        private const string _WELCOMEEMAILISHTML = "AISFAB_WelcomeEMailIsHTML";
        private const string _RESETEMAILSUBJECT = "AISFAB_ResetEMailSubject";
        private const string _RESETEMAILBODY = "AISFAB_ResetEMailBody";
        private const string _RESETEMAILISHTML = "AISFAB_ResetEMailIsHTML";

        private SPWeb _Web = null;

        public ConfigSettings(SPWeb web)
        {
            _Web = web;
        }

        private string GetProperty(string key)
        {
            string ReturnValue = null;
            try
            {
                ReturnValue = _Web.Properties[key];
            }
            catch { }
            return ReturnValue;
        }

        private void SetProperty(string key, string value)
        {
            _Web.Properties[key] = value;
        }

        /// <summary>
        /// Update the database with the property changes.
        /// Must be called once all properties have been updated.
        /// </summary>
        public void Update()
        {
            _Web.Properties.Update();
        }

        /// <summary>
        /// Configuration setting to automatically generate new user passwords
        /// Defaults to True
        /// </summary>
        public bool AutoGenNewUserPassword
        {
            get
            {
                bool Value;
                if (!Boolean.TryParse(GetProperty(_AUTOGENNEWUSERPASSSWORD), out Value))
                {
                    Value = true;
                }
                return Value;
            }
            set
            {
                SetProperty(_AUTOGENNEWUSERPASSSWORD, value.ToString());
            }
        }

        /// <summary>
        /// Configuration flag for auto generation of welcome e-mails.
        /// Defaults to True
        /// </summary>
        public bool AutoGenWelcomeEMail
        {
            get
            {
                bool Value;
                if (!Boolean.TryParse(GetProperty(_AUTOGENWELCOMEEMAIL), out Value))
                {
                    Value = true;
                }
                return Value;
            }
            set
            {
                SetProperty(_AUTOGENWELCOMEEMAIL, value.ToString());
            }
        }

        /// <summary>
        /// Configuration flag to enable / disable user password resets
        /// Defaults to True
        /// </summary>
        public bool EnableResetPassword
        {
            get
            {
                bool Value;
                if (!Boolean.TryParse(GetProperty(_ENABLEPASSWORDRESET), out Value))
                {
                    Value = true;
                }
                return Value;
            }
            set
            {
                SetProperty(_ENABLEPASSWORDRESET, value.ToString());
            }
        }

        /// <summary>
        /// Configuration flag to signal the welcome e-mail is HTML
        /// Defaults to true
        /// </summary>
        public bool WelcomeEMailIsHTML
        {
            get
            {
                bool Value;
                if (!Boolean.TryParse(GetProperty(_WELCOMEEMAILISHTML), out Value))
                {
                    Value = true;
                }
                return Value;
            }
            set
            {
                SetProperty(_WELCOMEEMAILISHTML, value.ToString());
            }
        }

        /// <summary>
        /// Configuration text for the welcome e-mail subject line
        /// Default value in resource file under key:Settings_WelcomeEMailSubject
        /// </summary>
        public string WelcomeEMailSubject
        {
            get
            {
                string Value = GetProperty(_WELCOMEEMAILSUBJECT);
                if (String.IsNullOrEmpty(Value)) 
                {
                    Value = Common.GetLocalizedResource("$Resources:Settings_WelcomeEMailSubject");
                }
                return Value;
            }
            set
            {
                SetProperty(_WELCOMEEMAILSUBJECT, value);
            }
        }

        /// <summary>
        /// Configuration text for the welcome e-mail body
        /// Default value in resource file under key:Settings_WelcomeEMailBody
        /// </summary>
        public string WelcomeEMailBody
        {
            get
            {
                string Value = GetProperty(_WELCOMEEMAILBODY);
                if (String.IsNullOrEmpty(Value))
                {
                    Value = Common.GetLocalizedResource("$Resources:Settings_WelcomeEMailBody");
                }
                return Value;
            }
            set
            {
                SetProperty(_WELCOMEEMAILBODY, value);
            }
        }

        /// <summary>
        /// Configuration flag to signal the reset e-mail is HTML
        /// Defaults to true
        /// </summary>
        public bool ResetEMailIsHTML
        {
            get
            {
                bool Value;
                if (!Boolean.TryParse(GetProperty(_RESETEMAILISHTML), out Value))
                {
                    Value = true;
                }
                return Value;
            }
            set
            {
                SetProperty(_RESETEMAILISHTML, value.ToString());
            }
        }

        /// <summary>
        /// Configuration text for the reset e-mail subject line
        /// Default value in resource file under key:Settings_ResetEMailSubject
        /// </summary>
        public string ResetEMailSubject
        {
            get
            {
                string Value = GetProperty(_RESETEMAILSUBJECT);
                if (String.IsNullOrEmpty(Value))
                {
                    Value = Common.GetLocalizedResource("$Resources:Settings_ResetEMailSubject");
                }
                return Value;
            }
            set
            {
                SetProperty(_RESETEMAILSUBJECT, value);
            }
        }

        /// <summary>
        /// Configuration text for the reset e-mail body
        /// Default value in resource file under key:Settings_ResetEMailBody
        /// </summary>
        public string ResetEMailBody
        {
            get
            {
                string Value = GetProperty(_RESETEMAILBODY);
                if (String.IsNullOrEmpty(Value))
                {
                    Value = Common.GetLocalizedResource("$Resources:Settings_ResetEMailBody");
                }
                return Value;
            }
            set
            {
                SetProperty(_RESETEMAILBODY, value);
            }
        }

    }
}
